<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title>PHP Class Structure Documentation</title>
    <link href="css/reset.css" media="" rel="stylesheet" type="text/css" />
    <link href="css/styles.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<?php

$str_example_group = isset($_REQUEST['group']) ? $_REQUEST['group'] : null;
$str_example_focus = isset($_REQUEST['example']) ? $_REQUEST['example'] : null;

$arr_navigation = array(
    'weaved' => 'Weaved',
    'mixin'  => 'Mixin'
);

?>
<body>
    <div class="header">
        <span><a href="?">PHP Class Structure Documentation</a></span>
        <span class="navigation">
        <?php
        foreach($arr_navigation as $str_key => $str_label) {
            $str_class = ($str_key == $str_example_group) ? ' class="active"' : '';
            echo '<a ' . $str_class . 'href="?group=' . $str_key . '">' . $str_label . '</a>';
        }
        ?>
        </span>
    </div>
    <?php
    
    //--------------------------------------------------------------------------
    // retrieve all the examples listed in the associated directory
    
    $str_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $str_example_group;
    $obj_directory = dir($str_directory);
    
    $arr_examples = array();
    if($obj_directory !== false) {
        while (false !== ($str_entry = $obj_directory->read())) {
            if(pathinfo($str_entry, PATHINFO_EXTENSION) == 'php' && ($str_entry[0] != '_')) {
                $arr_examples[$str_entry] = $obj_directory->path . DIRECTORY_SEPARATOR . $str_entry;
            }
        }
    }
    asort($arr_examples); 
    
    asort($arr_examples);
    
    //--------------------------------------------------------------------------
    // display side panel
    
    echo '<div class="panel">';
    echo '<ul class="navigation">';
    
    foreach($arr_examples as $str_key => $str_file) {
        $str_label = _example_label($str_key);
        
        $str_class = ($str_key == $str_example_focus) ? ' class="active"' : '';
        
        echo '<li' . $str_class .'><a href="?group=' . $str_example_group . '&amp;example=' . $str_key . '">' . $str_label . '</a></li>';
    }
    
    echo '</ul>';
    echo '</div>';
    
    //--------------------------------------------------------------------------
    // display the workspace
    
    echo '<div class="workspace">';
    
    $str_file = $arr_examples[$str_example_focus];
    
    if(file_exists($str_file)) {
        
        $str_example_label = _example_label($str_example_focus);
        echo '<h1>' . $str_example_label . '</h1>';
        
        echo '<div class="example">';
        
        // get the file contents
        $str_source = file_get_contents($str_file);
        $str_source = highlight_string($str_source, true);
        
        echo '<div class="block">';
        echo '<div class="label">Source Code</div>';
        echo '<div class="content">';
        echo '<div class="source">' . $str_source . '</div>';
        echo '</div>';
        echo '</div>';
        
        // get the file output (as if run from the command-line)
        $str_command = 'php ' . $str_file;
        $str_output = shell_exec($str_command);

        echo '<div class="block">';
        echo '<div class="label">Output</div>';
        echo '<div class="content">';
        echo '<div class="output">' . htmlentities($str_output) . '</div>';
        echo '</div>';
        echo '</div>';
        
        echo '</div>';
    }
        
    echo '</div>';
    
    ?>
</body>
</html>
<?php

function _example_label($str_file) {
    $int_start = strpos($str_file, '.')+1;
    $int_end   = strrpos($str_file, '.');
    $str_label = substr($str_file, $int_start, ($int_end - $int_start));
    $str_label = str_replace('-', ' ', $str_label);
    $str_label = ucwords($str_label);
    return $str_label;
}

?>
